-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for support Instance::of in configuration #21
Conversation
This breaks instantiation using the DI container, therefore it is a BC break. |
I didn't create an issue. I posted this PR instead. Should I create an issue? |
No but some more explanation in here would be nice. This change breaks some existing behaviour so it needs to be motivated. Can it be fixed in another way without breaking instantiation via the container? |
Why you think this breaks instantiation using the DI container? What do you mean? The tests in my project are still working correctly. DI work. |
I'm sorry I was unclear. Your issue can actually be solved by putting this in the DI config, this wouldn't require any change to the module code. \yii\web\Application::class => static function(Container $c, array $params): \yii\web\Application {
return new \yii\web\Application($params);
} |
My app config looks something like this. Did you mean this? [
'container' => [
'singletons' => [
// singletons
\yii\web\Application::class => static function(Container $c, array $params): \yii\web\Application {
return new \yii\web\Application($params);
},
'MyComponentClassFromDI' => [
'class' => app\components\MyComponentClassFromDI::class,
],
],
'definitions' => [
// definitions
],
],
'components' => [
'MyComponentClassFromDI' => \yii\di\Instance::of('MyComponentClassFromDI'),
],
// other application configuration things
]; |
I could be wrong, but i think at the time of the creation of the application itself, the DI container does not exist at all. Hence the problem with Instance::of(). |
That depends on what is in your bootstrap. So that is what will break with your change. |
Thank you very much. If done in the way you suggested, everything will work. |
This is subjective, others might not share my opinion. In my view the container consists outside the application and the method of configuring it via app config array is just a dirty hack so people can have all configuration in one place. https://github.com/yiisoft/yii2/blob/master/framework/Yii.php this file creates it as a side effect. I don't use that file but instead use my own bootstrap that instantiates the container. Imo that's cleaner |
@SamMousa , How do you like this new revision? I think it works for all options. |
It doesn't, I think you've misunderstood my issue. My DI container config tells the container how to create the application. So any creation that uses |
@SamMousa , Have you looked at the new code? The new version uses |
Hmm, github shows just 1 file changed... and that is only the |
Ah wait, now I get it, you have reverted your old change so this is the full feature. Yes, that looks good to me! You take out container config and use that directly; feels like a clean solution to me! |
Yes, i change only 1 file (src/Codeception/Lib/Connector/Yii2.php): if (isset($config['container']))
{
Yii::configure(Yii::$container, $config['container']);
unset($config['container']);
} |
Looks alright but we need to fix tests before merge to ensure it doesn't break things: #22 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase on master to get the tests passing.
Using createObject with \yii\di\Instance::of in configuration does not work. Creating an instance of the application in the suggested way solves the problem.
This reverts commit 01bf59e
Using createObject with \yii\di\Instance::of in configuration does not work. Creating an instance of the application in the suggested way solves the problem.
done |
Thanks. |
Using createObject with \yii\di\Instance::of in configuration does not work.
Creating an instance of the application in the suggested way solves the problem.